local fuzhu = fk.CreateSkill{
  name = "kl__fuzhu",
  tags = { Skill.Switch },
}


fuzhu:addEffect(fk.AfterCardsMove, {
  mute = true,
  anim_type = "switch",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(fuzhu.name) and player:usedSkillTimes(fuzhu.name, Player.HistoryRound) < 2 then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              local card = Fk:getCardById(info.cardId)
              return #player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
                for _, move2 in ipairs(e.data) do
                  if move2.from == player then
                    for _, info2 in ipairs(move2.moveInfo) do
                      if info2.fromArea == Card.PlayerHand or info2.fromArea == Card.PlayerEquip then
                        local card2 = Fk:getCardById(info2.cardId)
                        return card.type ~= card2.type
                      end
                    end
                  end
                end
              end, Player.HistoryTurn) > 0
            end
          end
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local prompt = "#kl__fuzhu-1"
    if player:getSwitchSkillState(fuzhu.name, false) == fk.SwitchYin then
      prompt = "#kl__fuzhu-2"
    end
    local to = player.room:askToChoosePlayers(player,{
      targets = player.room.alive_players,
      min_num = 1,
      max_num = 1,
      skill_name = fuzhu.name,
      prompt = prompt,
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, { to = to[1] })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local to = event:getCostData(self).to
    player.room:notifySkillInvoked(player, fuzhu.name)
    if player:getSwitchSkillState(fuzhu.name, true) == fk.SwitchYang then
      player:broadcastSkillInvoke(fuzhu.name, 1)
      to:drawCards(2, fuzhu.name)
    else
      player:broadcastSkillInvoke(fuzhu.name, 2)
      player.room:loseHp(to, 1, fuzhu.name)
    end
  end,
})


return fuzhu
